import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}

// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};
class Solution {
    //N叉树的层序遍历
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> ret= new ArrayList<>();
        if(root == null) return ret;
        Queue<Node> queue = new LinkedList<>();
        Node cur = root;
        queue.add(cur);
        int n = 1;
        while(!queue.isEmpty()){
            List<Integer> list = new ArrayList<>();
            int m = 0;//记录下一层的几点
            for(int i = 0; i < n; i++){
                Node node = queue.poll();
                list.add(node.val);
                //孩子节点不为空, 把孩子节点放入队列
                for (Node child : node.children) {
                    if(child != null)
                        queue.add(child);
                }
                m += node.children.size();
            }
            n = m;
            ret.add(list);
        }
        return ret;
    }
}